#!/usr/bin/env python
# encoding: utf-8
# @author: Lin Han
# @contact: voldemort22@126.com
# @file: background_update.py
# @time: 2021/5/18 13:29
# @desc:
import sys
import time
import traceback

import json

import docker


from background.crud import *
from cache.redis_pool import get_redis
from docker_action.kill_and_restart import restart_container
from views.crud import send_to_message_queue


def update_01():
    try:
        restart_container(name="history_clamp", port=HISTORY_CLAMP_REDIS_PORT)
        # redis_connection = redis.StrictRedis(host=REDIS_IP, port=HISTORY_CLAMP_REDIS_PORT, db=0)
        # redis_connection.set("stop-writes-on-bgsave-error", json.dumps("no"))
        extract_history_clamp_from_mongo_to_redis(port=HISTORY_CLAMP_REDIS_PORT)
    except:
        traceback.print_exc()
    try:
        udpate_history_clamp_data_board(port=HISTORY_CLAMP_REDIS_PORT)
    except:
        traceback.print_exc()


def update_02():
    try:
        restart_container(name="history_part", port=HISTORY_PART_REDIS_PORT)
        redis_connection = redis.StrictRedis(host=REDIS_IP, port=HISTORY_PART_REDIS_PORT, db=0)
        history_part_config = retrieve_all_mongo_values({"type": CONFIG_DATA_HISTORY_PART_TYPE}, collection_name=CONFIG_COLLECTION)[0][CONFIG_DATA]
        redis_connection.set("history_part_config", json.dumps(history_part_config))
        data = retrieve_all_mongo_values({}, collection_name=HISTORY_PART_COLLECTION)
        data_keys = [i["number"] for i in data]
        redis_connection.set(HISTORY_PART_COLLECTION, json.dumps(data_keys))
        for i in data:
            redis_connection.set(i["number"], json.dumps(i))
    except:
        traceback.print_exc()


def update_03():
    try:
        restart_container(name="standard_clamp", port=STANDARD_CLAMP_REDIS_PORT)
        # redis_connection = redis.StrictRedis(host=REDIS_IP, port=STANDARD_CLAMP_REDIS_PORT, db=0)
        # redis_connection.set("stop-writes-on-bgsave-error", json.dumps("no"))
        extract_standard_clamp_from_mongo_to_redis(port=STANDARD_CLAMP_REDIS_PORT)
    except:
        traceback.print_exc()


def update_04():
    try:
        restart_container(name="standard_part", port=STANDARD_PART_REDIS_PORT)
        # redis_connection = redis.StrictRedis(host=REDIS_IP, port=STANDARD_PART_REDIS_PORT, db=0)
        # redis_connection.set("stop-writes-on-bgsave-error", json.dumps("no"))
        update("standard_part", collection_name=STANDARD_PART_COLLECTION, port=STANDARD_PART_REDIS_PORT)
    except:
        traceback.print_exc()


def update_05():
    try:
        restart_container(name="parameter_model", port=PARAMETER_MODEL_REDIS_PORT)
        # redis_connection = redis.StrictRedis(host=REDIS_IP, port=PARAMETER_MODEL_REDIS_PORT, db=0)
        # redis_connection.set("stop-writes-on-bgsave-error", json.dumps("no"))
        update("parameter_model", collection_name=PARAMETER_MODEL_COLLECTION, port=PARAMETER_MODEL_REDIS_PORT)
    except:
        traceback.print_exc()


def update_06():
    try:
        restart_container(name="scene", port=SCENE_REDIS_PORT)
        # redis_connection = redis.StrictRedis(host=REDIS_IP, port=SCENE_REDIS_PORT, db=0)
        # redis_connection.set("stop-writes-on-bgsave-error", "no")
        extract_scene_from_mongo_to_redis(port=SCENE_REDIS_PORT)
    except:
        traceback.print_exc()


def update_all_redis():
    update_01()
    print(f"更新完01数据！")

    update_02()
    print(f"更新完02数据！")

    # update_03()
    # print(f"更新完03数据！")

    update_04()
    print(f"更新完04数据！")

    update_05()
    print(f"更新完05数据！")

    update_06()
    print(f"更新完06数据！")


def transfer_untransfered_02():
    history_parts = retrieve_all_mongo_values({}, collection_name=HISTORY_PART_COLLECTION)
    for i in history_parts:
        if "gltfAliOssAddress" not in i["file"].keys():
            send_to_message_queue(i["number"])


if __name__ == '__main__':

    # redis_connection = redis.StrictRedis(host=REDIS_IP, port=PARAMETER_MODEL_REDIS_PORT, db=0)
    # redis_connection.set("stop-writes-on-bgsave-error", json.dumps("no"))
    # print(redis_connection.get("stop-writes-on-bgsave-error"))
    # transfer_untransfered_02()
    # while True:
    #     # update_all_redis()
    #     update_06()
    #
    #     time.sleep(3600)
    import time
    start = time.time()

    print(f"格林威治时间{start}开始转化：")
    update_all_redis()

    print(time.time() - start)
